﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.Common;
using EShop.Model;
using EShop.BLL;


namespace EShop.Client
{
    public partial class frmPermissionManger : frmBase
    {
        private string _strEmployeeID = string.Empty;

        public string strEmployeeID
        {
            get { return _strEmployeeID; }
            set { _strEmployeeID = value; }
        }

        public frmPermissionManger()
        {
            InitializeComponent();
        }
        public frmPermissionManger(string strEmployeeID)
        {
            InitializeComponent();
            this.strEmployeeID = strEmployeeID;
        }

        private void frmPermissionManger_Load(object sender, EventArgs e)
        {

            //权限检查
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.ToolManager))
            {
                MessageBox.Show("您没有相应权限操作！");
                this.Close();
            }

            initBindEmployeeData();
        }

        void initBindEmployeeData()
        {
            List<EmployeeInfo> aEmployeeList = new EmployeeBLL().GetEmployeePermission();
            foreach (EmployeeInfo employeeInfo in aEmployeeList)
            {
                combox_Employee.Items.Add(new EmployeePermissionItem(employeeInfo.ID,employeeInfo.FullName,employeeInfo.EmployeePermission));
            }
            if (!string.IsNullOrEmpty(strEmployeeID))
            {
                int i = -1;
                foreach (EmployeeInfo item in aEmployeeList)
                {
                    i++;
                    if (item.EmployeeID ==strEmployeeID)
                    {
                        this.combox_Employee.SelectedIndex = i;
                        this.combox_Employee.Enabled = false;
                        break;
                    }
                }
            }
        }


        private class EmployeePermissionItem
        {
            public string ID;
            public string FullName;
            public PermissionEnum Permission;
            public EmployeePermissionItem(string id,string fullName,PermissionEnum permission)
            {
                this.ID = id;
                this.FullName = fullName;
                this.Permission = permission;
            }

            public override string ToString()
            {
                return this.FullName;
            }
        }

        private void combox_Employee_SelectedIndexChanged(object sender, EventArgs e)
        {
            EmployeePermissionItem selectItem = combox_Employee.SelectedItem as EmployeePermissionItem;

            ClearPermission();

            if (selectItem.Permission == PermissionEnum.None)
                chk_None.Checked = true;

            if (CheckPermission(selectItem.Permission, PermissionEnum.CashManager))
                chk_CashManager.Checked = true;
            if (CheckPermission(selectItem.Permission, PermissionEnum.MemberManager))
                chk_MemberManager.Checked = true;
            if (CheckPermission(selectItem.Permission, PermissionEnum.MerchandiseManager))
                chk_MerchandiseManager.Checked = true;
            if (CheckPermission(selectItem.Permission, PermissionEnum.BaseManager))
                chk_BaseManager.Checked = true;
            if (CheckPermission(selectItem.Permission, PermissionEnum.EmployeeManager))
                chk_EmployeeManager.Checked = true;
            if (CheckPermission(selectItem.Permission, PermissionEnum.ToolManager))
                chk_ToolManager.Checked = true;
        }

        void ClearPermission()
        {
            chk_None.Checked = false;
            chk_CashManager.Checked = false;
            chk_MemberManager.Checked = false;
            chk_MerchandiseManager.Checked = false;
            chk_BaseManager.Checked = false;
            chk_EmployeeManager.Checked = false;
            chk_ToolManager.Checked = false;
        }

        private void btnConform_Click(object sender, EventArgs e)
        {
            PermissionEnum selectPermission = PermissionEnum.None;
            if(!chk_None.Checked)
            {
                if (chk_CashManager.Checked)
                    selectPermission = SystemLibrary.AddPermission(selectPermission, PermissionEnum.CashManager);
                if(chk_MemberManager.Checked)
                    selectPermission = SystemLibrary.AddPermission(selectPermission, PermissionEnum.MemberManager);
                if (chk_MerchandiseManager.Checked)
                    selectPermission = SystemLibrary.AddPermission(selectPermission, PermissionEnum.MerchandiseManager);
                if (chk_BaseManager.Checked)
                    selectPermission = SystemLibrary.AddPermission(selectPermission, PermissionEnum.BaseManager);
                if (chk_EmployeeManager.Checked)
                    selectPermission = SystemLibrary.AddPermission(selectPermission, PermissionEnum.EmployeeManager);
                if (chk_ToolManager.Checked)
                    selectPermission = SystemLibrary.AddPermission(selectPermission, PermissionEnum.ToolManager);
            }

            EmployeePermissionItem selectItem = combox_Employee.SelectedItem as EmployeePermissionItem;
            if(selectItem.Permission != selectPermission)
            {
                try
                {
                    int iPermissionValue = SystemLibrary.PermissionToInt(selectPermission);
                    bool blResult = new EmployeeBLL().UpdateEmployeePermission(selectItem.ID, iPermissionValue);
                    if (blResult)
                    {
                        MessageBox.Show("更新成功！");
                    }
                    else
                    {
                        MessageBox.Show("更新失败！");
                    }
                }
                catch (Exception ex)
                {
                    LogWrite(ex);
                }
                
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void chk_None_CheckedChanged(object sender, EventArgs e)
        {
            chk_CashManager.Checked = false;
            chk_MemberManager.Checked = false;
            chk_MerchandiseManager.Checked = false;
            chk_BaseManager.Checked = false;
            chk_EmployeeManager.Checked = false;
            chk_ToolManager.Checked = false;
        }

        private void chkNotNone_CheckedChanged(object sender, EventArgs e)
        {
            chk_None.Checked = false;
            //CheckBox chk = (CheckBox) sender;
            //chk.Checked = !chk.Checked;
            //return;
        }

    }
}
